#include<iostream>
#include<windows.h>
#include "threadPool.h"
using namespace std;

int main()
{	
	::srand(time(NULL));
	TaskQueue tq(3);			//任务队列长度
	ThreadPool pool(5);			//线程池个数
	pool.start([&]() 
		{
			Sleep(1000*2);
			int i = 1;
			while (1)
			{
				int task;
				{
					unique_lock<mutex> lock(pool.loc);
					while (tq._is_empty() && pool._is_runing)pool.R_ONLY.wait(lock);
					if (tq._is_empty())break;
					task = tq.get();
					cout << this_thread::get_id()<<"第" << i++ << " 次获取: " << task << endl;
				}
				pool.W_ONLY.notify_one();
				Sleep(1000);
			}
		});
	int times = 7;
	int i = 1;
	while (times)
	{
		int a = ::rand() % 100;
		cout << "第" << i++ << "次生产" << a << endl;
		{
			unique_lock<mutex> lock(pool.loc);
			if (tq._is_full())pool.W_ONLY.wait(lock);
			tq.push(a);
		}
		pool.R_ONLY.notify_all();
		times--;
	}; 
	pool.stop();
	cout << "main is over" << endl;
}